package cc.messcat.service.fupin;

import cc.messcat.common.service.BaseServiceImpl;
import cc.messcat.common.util.BeanUtil;
import cc.messcat.dao.fupin.FamilyDao;
import cc.messcat.dao.fupin.PoolVisionDao;
import cc.messcat.dao.fupin.VisionDao;
import cc.messcat.entity.fupin.Family;
import cc.messcat.entity.fupin.Vision;
import cc.messcat.entity.fupin.VisionPoolFamily;
import cc.messcat.entity.vo.VisionPoolFamilyVo;
import cc.messcat.util.Collections;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 2018/4/27
 */
@Service
public class PoolVisionServiceImpl extends BaseServiceImpl<VisionPoolFamily> implements PoolVisionService {
    @Autowired
    private PoolVisionDao poolVisionDao;
    @Autowired
    private FamilyDao familyDao;

    
    @Override
    public Long merge(VisionPoolFamily poolVision) {
        return poolVisionDao.merge(poolVision);
    }

    /**
     * 根据贫困户id和上传日期获取街景
     */
    @Override
    public List<VisionPoolFamilyVo> listVision(Long poolFamilyId, String date, Integer pageNo, Integer pageSize) {
    	List<VisionPoolFamily> list = null;
    	List<VisionPoolFamilyVo> result = new ArrayList<>();
        if(pageNo == null || pageSize == null) {
            String dateCondition  = StringUtils.isNotBlank(date) ? " and DATE_FORMAT(createTime,'%Y-%m-%d') =:date " : "";
            String hql = " from VisionPoolFamily where poolFamilyId=:poolFamilyId "+ dateCondition +" order by createTime desc ";
            list = poolVisionDao.queryList(hql,Collections.<String,Long>generateHashMap("poolFamilyId",poolFamilyId,"date",date));
            //return null;
        }else{
        	 String dateCondition  = StringUtils.isNotBlank(date) ? " and DATE_FORMAT(createTime,'%Y-%m-%d') =? " : "";
             String hql = " from VisionPoolFamily where poolFamilyId=? "+ dateCondition +" order by createTime desc ";
             if(StringUtils.isNotBlank(date)){
             	list = poolVisionDao.findPageObjects(hql,pageNo,pageSize,poolFamilyId,date);
             }else{
             	list = poolVisionDao.findPageObjects(hql,pageNo,pageSize,poolFamilyId);
             }
        }

        for (VisionPoolFamily visionPoolFamily : list) {
        	VisionPoolFamilyVo visionPoolFamilyVo = new VisionPoolFamilyVo();
        	BeanUtils.copyProperties(visionPoolFamily, visionPoolFamilyVo);
        	Long pfId = visionPoolFamilyVo.getPoolFamilyId();
        	//根据贫困户id查询户主
        	Family family = familyDao.findRelationByFid(pfId);
        	if(family !=null){
        		visionPoolFamilyVo.setRelation(family.getRelation());
        		visionPoolFamilyVo.setName(family.getName());
        	}
        	result.add(visionPoolFamilyVo);
		}
        		
        return result;
    }

    @Override
    public int count(Long poolFamilyId, String date) {
        String dateCondition  = StringUtils.isNotBlank(date) ? " and DATE_FORMAT(createTime,'%Y-%m-%d') = '"+date+"'" : "";
        return ((Long)poolVisionDao.find("select count(*) from VisionPoolFamily where  poolFamilyId="+poolFamilyId + dateCondition).get(0)).intValue();
    }

    @Override
    public List<String> listVisionDateByPoolFamilyId(Long poolFamilyId ) {
        String hql = "select distinct(DATE_FORMAT(createTime,'%Y-%m-%d')) from VisionPoolFamily where poolFamilyId=:poolFamilyId order by createTime desc ";
        return poolVisionDao.queryList(hql,Collections.<String,Long>generateHashMap("poolFamilyId",poolFamilyId));
    }

}
